package com.example.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
/**
 * @ClassName TaskPoolConfig
 * @Description TODO
 * @Author jeffrey
 * @Date 2023/10/08 16:57
 * @Version 1.0
 **/
@Configuration
@EnableAsync  //开启异步服务
public class TaskPoolConfig {
    @Bean("taskExecutor")  //线程池对象名
    public Executor taskExecutor(){
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(10);//设置核心线程数
        executor.setMaxPoolSize(50);//设置最大线程数
        executor.setQueueCapacity(200);//线程池组赛队列的容量
        executor.setKeepAliveSeconds(60);//线程生存期
        executor.setThreadNamePrefix("taskExecutor-"); //线程命名前缀
        executor.setWaitForTasksToCompleteOnShutdown(true); //线程池处理线程方案
        executor.setAwaitTerminationSeconds(60);  //线程池结束等待时间
        return executor;
    }
}
